#ifndef REPLAY_CENTER_H
#define REPLAY_CENTER_H

#include <QWidget>
#include <QDesktopWidget>
#include <QToolButton>
#include <QDebug>
#include <QLabel>
#include <QDesktopWidget>
#include <QBrush>
#include <QPoint>
#include <QCursor>
#include <QPainter>
#include "timepainter.h"
#include <QTimer>
#include <QDate>
#include "com/log.h"
#include <QList>
#include <QTcpServer>
#include <QTcpSocket>
#include "hint_Dlg.h"
#include "math.h"
#include "cstring"
#include "cstdio"

#define DATA_ROW_HEIGHT (12)
#define EMPTY_ROW_HEIGHT (2)
#define CELL_COLUMN_WIDTH (2)


#define RPLY_CUCOL_BACKCOLOR (QColor(0xFF,0x0,0x0))
#define ROW_TIMING_DATA_BACKCOLOR (QColor(0x42,0xd0,0x19))
#define ROW_ALARM_DATA_BACKCOLOR (QColor(0xff,0x97,0x00))
#define ROW_NORECORD_BACKCOLOR (QColor(0x80,0x80,0x80))
#define ROW_HGAP_BACKCOLOR (QColor(43,54,67))
#define ROW_VGAP_BACKCOLOR (QColor(0x80,0x80,0x80))
//#define ROW_VGAP_BACKCOLOR (QColor(0xC0,0xC0,0xC0))


typedef enum _pbr_zoom{
    pbrZoom_24H=24/*1-bigCell 1h*/,
    pbrZoom_6H=24*4/*1-bigCell 15M*/,
    pbrZoom_1H=24*4*3/*1-bigCell 5M*/,
    pbrZoom_1M=24*60/*1-bigCell 2.5M*/
}pbrZoom;


typedef enum _rply_speed{
    Rply_Speed_116=0,
    Rply_Speed_108=1,
    Rply_Speed_104=2,
    Rply_Speed_102=3,
    Rply_Speed_1=4,
    Rply_Speed_2=5,
    Rply_Speed_4=6,
    Rply_Speed_8=7,
    Rply_Speed_16=8
}rplySpeed;

typedef enum _rply_status{
    Rply_Playing=0,
    Rply_Stop=1
}rplyStatus;



typedef struct _recordCellInfo{
    int startCell;
    int endCell;
}recordCellInfo;


typedef struct _replayCtl{
    rplySpeed   speed;
    int         current_Cell;
    int         next_Cell;
    rplyStatus  status;
}replayTimerInfo;


typedef struct {
    int                     index;
    int                     ch;
    rplySpeed               rply_Speed;
    QTimer*                 rply_timer;
    QDateTime               rply_startTime;
    QDateTime               rply_endTime;

    QList <h_records>       timing_rTList;
    QList <recordCellInfo>  timing_rCList;

    QList <h_records>       alarm_rTList;
    QList <recordCellInfo>  alarm_rCList;
}recordInfo;

typedef enum rec_Type{
  REC_TIMING=0,
  REC_ALARM=1
}REC_Type;


namespace Ui {
    class replayCenter;
}

class replayCenter : public QWidget
{
    Q_OBJECT

public:
    explicit replayCenter(QWidget *parent = 0);
    ~replayCenter();
    static replayCenter *Instance() {
        if (!m_instance) {
            m_instance = new replayCenter;
        }
        m_instance->show();
        return m_instance;
    }

    void playback_show();
    void playback_hide();

protected:
    void mousePressEvent(QMouseEvent*event);

public:
signals:
   void  win_trans(int win_id,bool flag);
   void  win_change(int win_id,int win_num);
   void  dispWhichWidget(int page);
   void  httpReq_signal(bool https,QString ip,QString url,int port,QJsonObject postData);
   void  reply_exitSignal();
private slots:
    void  rply_processNotifyTcp_slot();
    void  rply_TcpRecv_slot();
    void on_playback_process_tbW_cellEntered(int row, int column);


    void on_playbackCtl_Zoom24H_pBt_clicked();
    void on_playbackCtl_Zoom12H_pBt_clicked();
    void on_playbackCtl_Zoom4H_pBt_clicked();
    void on_playbackCtl_Zoom2H_pBt_clicked();
    void on_playbackCtl_SpeedAdd_pBt_clicked();
    void on_playbackCtl_SpeedSub_pBt_clicked();


    void on_playback_process_tbW_doubleClicked(const QModelIndex &index);

    void httpRply_slots(QString url, QJsonDocument rspData);
    void ctrlTimertimeout_slot();
    void mouseTimertimeout_slot();


    void on_Replay_calendarWidget_selectionChanged();

    void on_ReplaySwitch_cBx1_1_currentIndexChanged(int index);

    void on_playbackCtl_Start_pBt_clicked();

    void on_playbackCtl_Stop_pBt_clicked();


    void on_playbackCtl_Date_pBt_clicked();
    void on_recordType_cBx_currentIndexChanged(const QString &arg1);


    void on_Replay_Close_tBt_clicked();

private:
    void replay_IamAlive();
    void replay_queryProcess();
    void replay_timerRestart();
    void replay_initUI();
    void replay_initData();
    void replay_initConnect();
    void replay_initProcessTbW();
    void paintEvent(QPaintEvent *Event );

    void replay_rePaint_ByRECType(REC_Type type);
    void replay_rePaint_CurrentData();



    void  playback_ZoomX_24H();
    void  playback_ZoomX_12H();
    void  playback_ZoomX_4H();
    void  playback_ZoomX_2H();
    void  playback_stop();
    void  playback_Pause();
    void  playback_Play(int ch,int outch);
    void  playback_initAlarmPara();



    void  replay_selectTimeToReplay(int col);
    void  replay_drawRecordData(REC_Type type,char *stime,char * etime,QBrush recColor);

    void  recordCellInfoCpy(QList <recordCellInfo> *dst,QList <recordCellInfo> src);
    void  recordListInfoCpy(QList <h_records> *dst,QList <h_records> src);

    void replay_recordQuery(int ch);
    void replay_startQuery();
private:
    Ui::replayCenter*       ui;

    bool                    m_replayRecordQuery=false;
    bool                    m_replayCtl=false;

    static replayCenter*    m_instance;     //实例对象

    QDate                   m_reply_SelectDate;
    int                     m_tbW_height_deault=20;
    int                     m_tbW_width;
    int                     m_tbW_height=0;
    int                     m_scrolBarHeight=20;

    int                     m_bigCell_seconds;
    int                     m_smlCellNum_InbigCell;
    float                   m_smlCell_seconds;

    int                     m_tbW_col;
    const int               m_tbW_TimeRowNum=1;///FIXED
    const int               m_tbW_DataRowNum=1;////
    int                     m_tbW_EmptyRowNum=1;
    int                     m_tbW_row=2;

    int                     m_MouseIn_col;///disp Mouse pos time
    int                     m_MouseTmp_col;///disp Mouse pos time
    int                     m_MouseTimeOffset=9;
    QTimer                  m_MouseTimer;///chNum=1:Total Process Start;chNum=4:ch_1 Process Ctl Start;chNum>4:ch_x Process Ctl Start
    bool                    m_MousMove=false;


    TimePainter*            m_timeLinePainter=NULL;///timeLine
    pbrZoom                 m_pbrZoom=pbrZoom_24H;///zoom para


    const int               m_Replay_Row=1;///1(1-0) 2(1-0,3-1) 3(1-0,3-1,5-2) 4(1-0,3-1,5-2,7-3)
    QString                 m_Replay_StartTime;
    QString                 m_Replay_EndTime;


    int                     m_replay_totalChs=1;
    int                     m_rplyGetResultTimes=1;



    recordInfo              m_RecordInfoArray[MAX_REPLAY_WIN_NUM];
    replayTimerInfo         m_replayTimerCtl[MAX_REPLAY_WIN_NUM];

    rplyStatus              m_rplyStaus=Rply_Stop;
    int                     m_RplySpeed=Rply_Speed_1;
    int                     m_rplyIndex;
    int                     m_rply_ch=0;
    int                     m_rply_outch=0;

    QColor                  m_originalColBackColor;
    unsigned int            m_alarmRecIndex=0;
    qint64                  m_alarmTimes=0;
    qint64                  m_alarmSpeed=0;
    qint64                  m_alarmStep=0;
    unsigned int            m_ctrTimer_Interval;
    unsigned int            m_ctrTimer_timeOutTimes=0;
    unsigned int            m_queryProcess_Times=3;
    QTimer                  m_ctrlTimer_Timing;///chNum=1:Total Process Start;chNum=4:ch_1 Process Ctl Start;chNum>4:ch_x Process Ctl Start


    QTcpServer*             m_rplyProcessTcpSrv;
    QTcpSocket*             m_client_sock;
};
#endif // REPLAY_CENTER_H
